package com.ruoyi.course.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.course.domain.MySelectedCourse;
import com.ruoyi.course.service.ICourseSelectionService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.SelectCourse;
import com.ruoyi.course.service.ISelectCourseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * selectCourseController
 * 
 * @author Weiney
 * @date 2025-02-24
 */
@RestController
@RequestMapping("/course/selectCourse")
public class SelectCourseController extends BaseController
{
    @Autowired
    private ISelectCourseService selectCourseService;

    @Autowired
    private ICourseSelectionService courseSelectionService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询selectCourse列表
     */
    @PreAuthorize("@ss.hasPermi('course:selectCourse:list')")
    @GetMapping("/list")
    public TableDataInfo list(SelectCourse selectCourse)
    {
        startPage();
        SysDept sysDept = deptService.selectDeptById(getDeptId()); // 根据当前用户的部门id查询 部门对象
        // 再查询当前用户部门的父部门id，使当前班级的学生只能查询到本院（父部门）的选课信息
        selectCourse.setDeptId(sysDept.getParentId());
        List<SelectCourse> list = selectCourseService.selectSelectCourseList(selectCourse);
        return getDataTable(list);
    }

    /**
     * 导出selectCourse列表
     */
    // @PreAuthorize("@ss.hasPermi('course:selectCourse:export')")
    // @Log(title = "selectCourse", businessType = BusinessType.EXPORT)
    // @PostMapping("/export")
    // public void export(HttpServletResponse response, SelectCourse selectCourse)
    // {
    //     List<SelectCourse> list = selectCourseService.selectSelectCourseList(selectCourse);
    //     ExcelUtil<SelectCourse> util = new ExcelUtil<SelectCourse>(SelectCourse.class);
    //     util.exportExcel(response, list, "selectCourse数据");
    // }

    /**
     * 获取selectCourse详细信息
     */
    // @PreAuthorize("@ss.hasPermi('course:selectCourse:query')")
    // @GetMapping(value = "/{courseId}")
    // public AjaxResult getInfo(@PathVariable("courseId") Long courseId)
    // {
    //     return success(selectCourseService.selectSelectCourseByCourseId(courseId));
    // }

    /**
     * 新增selectCourse
     */
    // @PreAuthorize("@ss.hasPermi('course:selectCourse:add')")
    // @Log(title = "selectCourse", businessType = BusinessType.INSERT)
    // @PostMapping
    // public AjaxResult add(@RequestBody SelectCourse selectCourse)
    // {
    //     return toAjax(selectCourseService.insertSelectCourse(selectCourse));
    // }

    /**
     * 修改selectCourse
     */
    // @PreAuthorize("@ss.hasPermi('course:selectCourse:edit')")
    // @Log(title = "selectCourse", businessType = BusinessType.UPDATE)
    // @PutMapping
    // public AjaxResult edit(@RequestBody SelectCourse selectCourse)
    // {
    //     return toAjax(selectCourseService.updateSelectCourse(selectCourse));
    // }

    /**
     * 删除selectCourse
     */
    // @PreAuthorize("@ss.hasPermi('course:selectCourse:remove')")
    // @Log(title = "selectCourse", businessType = BusinessType.DELETE)
	// @DeleteMapping("/{courseIds}")
    // public AjaxResult remove(@PathVariable Long[] courseIds)
    // {
    //     return toAjax(selectCourseService.deleteSelectCourseByCourseIds(courseIds));
    // }

    /**
     * 查询选课状态
     *
     * @param courseId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('course:selectCourse:queryStatus')")
    @GetMapping(value = "/courseSelection/{courseId}")
    public AjaxResult getCourseSelectionStatus(@PathVariable("courseId") Long courseId) {
        Long userId = getUserId();
        boolean isSelected = courseSelectionService.getCourseSelectionStatus(courseId, userId);
        return success(isSelected);
    }

    /**
     * 选择课程
     */
    @PreAuthorize("@ss.hasPermi('course:selectCourse:confirm')")
    @Log(title = "selectCourse", businessType = BusinessType.INSERT)
    @PostMapping("/courseSelection")
    public AjaxResult confirmSelection(@RequestParam Long courseId)
    {
        MySelectedCourse mySelectedCourse = new MySelectedCourse();
        mySelectedCourse.setCourseId(courseId);
        mySelectedCourse.setUserId(getUserId());
        mySelectedCourse.setDeptId(getDeptId());
        return toAjax(courseSelectionService.insertCourseSelection(mySelectedCourse));
    }

    /**
     * 取消选课
     */
    @PreAuthorize("@ss.hasPermi('course:selectCourse:cancel')")
    @Log(title = "selectCourse", businessType = BusinessType.DELETE)
    @DeleteMapping("/courseSelection/{courseId}")
    public AjaxResult cancelSelection(@PathVariable Long courseId)
    {   Long userId = getUserId();
        return toAjax(courseSelectionService.deleteCourseSelectionByCourseIdAndUserId(courseId, userId));
    }
}
